Skip to content

MkLlm

Show source on GitHub

Node for LLM-based text generation.

Example: Regular

Jinja

{{ "Write a poem about MkDocs" | MkLlm(model="ollama/smollm2:360m") }}

Python

MkLlm('Write a poem about MkDocs', 'ollama/smollm2:360m', [])

MkDocs, a place where minds meet, Where words echo and whispers flow. A network of thoughts that weave the web, In this realm, ideas reside.

They stand ready to form and mold, With words and messages so true. Together, they create something new, A symphony of thought, in a big way.

Each word is a brushstroke on the page, Reflection of a moment's breath. The network's threads converge, Connecting minds that flow.

They touch hearts that need to mend, And wounds that cannot heal. Their words are compassion's gentle hand, A balm for those who seek peace.

In this space where thoughts and souls, MkDocs offer guidance too. Their messages carry hope and light, Illuminating paths to find.

Together, they form a bond so strong, In the realm of words they grow. They touch hearts that whisper secrets, In this place where minds meet.

MkDocs, a place where minds meet,
Where words echo and whispers flow.
A network of thoughts that weave the web,
In this realm, ideas reside.

They stand ready to form and mold,
With words and messages so true.
Together, they create something new,
A symphony of thought, in a big way.

Each word is a brushstroke on the page,
Reflection of a moment's breath.
The network's threads converge,
Connecting minds that flow.

They touch hearts that need to mend,
And wounds that cannot heal.
Their words are compassion's gentle hand,
A balm for those who seek peace.

In this space where thoughts and souls,
MkDocs offer guidance too.
Their messages carry hope and light,
 Illuminating paths to find.

Together, they form a bond so strong,
In the realm of words they grow.
They touch hearts that whisper secrets,
In this place where minds meet.
<p>MkDocs, a place where minds meet,
Where words echo and whispers flow.
A network of thoughts that weave the web,
In this realm, ideas reside.</p>
<p>They stand ready to form and mold,
With words and messages so true.
Together, they create something new,
A symphony of thought, in a big way.</p>
<p>Each word is a brushstroke on the page,
Reflection of a moment's breath.
The network's threads converge,
Connecting minds that flow.</p>
<p>They touch hearts that need to mend,
And wounds that cannot heal.
Their words are compassion's gentle hand,
A balm for those who seek peace.</p>
<p>In this space where thoughts and souls,
MkDocs offer guidance too.
Their messages carry hope and light,
 Illuminating paths to find.</p>
<p>Together, they form a bond so strong,
In the realm of words they grow.
They touch hearts that whisper secrets,
In this place where minds meet.</p>

Bases: MkText

text property

text: str

__init__

__init__(
    user_prompt: str,
    system_prompt: str | None = None,
    model: str = "gpt-3.5-turbo",
    context: str | None = None,
    extra_files: Sequence[str | PathLike[str]] | None = None,
    **kwargs: Any
)

Parameters:

Name Type Description Default
user_prompt str

Main prompt for the LLM

required
system_prompt str | None

System prompt to set LLM behavior

None
model str

LLM model identifier to use

'gpt-3.5-turbo'
context str | None

Main context string

None
extra_files Sequence[str | PathLike[str]] | None

Additional context files or strings

None
kwargs Any

Keyword arguments passed to parent

{}
Name Children Inherits
MkText
mknodes.basenodes.mktext
Class for any Markup text.
graph TD
  94721312298960["mkllm.MkLlm"]
  94721309096560["mktext.MkText"]
  94721308848336["mknode.MkNode"]
  94721311766592["node.Node"]
  140564252373184["builtins.object"]
  94721309096560 --> 94721312298960
  94721308848336 --> 94721309096560
  94721311766592 --> 94721308848336
  140564252373184 --> 94721311766592
/home/runner/work/mknodes/mknodes/mknodes/templatenodes/mkllm/metadata.toml
[metadata]
icon = "mdi:view-grid"
status = "new"
name = "MkLlm"

[examples.regular]
title = "Regular"
jinja = """
{{ "Write a poem about MkDocs" | MkLlm(model="ollama/smollm2:360m") }}
"""

# [output.markdown]
# template = """
# <div class="grid cards" markdown="1">

# {% for item in node.items %}
# -   {{ item | indent }}
# {% endfor %}
# </div>
# """
mknodes.templatenodes.mkllm.MkLlm
class MkLlm(mktext.MkText):
    """Node for LLM-based text generation."""

    ICON = "material/format-list-group"
    REQUIRED_PACKAGES = [resources.Package("litellm")]

    def __init__(
        self,
        user_prompt: str,
        system_prompt: str | None = None,
        model: str = "gpt-3.5-turbo",
        context: str | None = None,
        extra_files: Sequence[str | os.PathLike[str]] | None = None,
        **kwargs: Any,
    ):
        """Constructor.

        Args:
            user_prompt: Main prompt for the LLM
            system_prompt: System prompt to set LLM behavior
            model: LLM model identifier to use
            context: Main context string
            extra_files: Additional context files or strings
            kwargs: Keyword arguments passed to parent
        """
        super().__init__(**kwargs)
        self.user_prompt = user_prompt
        self.system_prompt = system_prompt
        self._model = model
        self._context = context
        self._extra_files = extra_files or []

    def _process_extra_files(self) -> list[str]:
        """Process extra context items, reading files if necessary.

        Returns:
            List of context strings.
        """
        context_items: list[str] = []

        def process_dir(path: UPath) -> list[str]:
            return [f.read_text() for f in path.rglob("*") if f.is_file()]

        for item in self._extra_files:
            try:
                path = UPath(item)
                if path.is_file():
                    context_items.append(path.read_text())
                elif path.is_dir():
                    context_items.extend(process_dir(path))
                else:
                    context_items.append(str(item))
            except Exception as exc:
                err_msg = f"Failed to read context file: {item}"
                logger.warning(err_msg)
                raise ValueError(err_msg) from exc

        return context_items

    @property
    def text(self) -> str:
        """Generate text using the LLM.

        Returns:
            Generated text content.
        """
        context_items = self._process_extra_files()
        combined_context = (
            "\n".join(filter(None, [self._context, *context_items])) or None
        )

        return complete_llm(
            self.user_prompt,
            self.system_prompt,
            model=self._model,
            context=combined_context,
        )